package talking.frame;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ServerSocket;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import talking.thread.ReceiveNews;

/**
 * 服务器窗体
 * @create 2020.5.21
 * @author Administrator
 *
 */
public class MainFrame extends JFrame implements ActionListener{

	//关闭服务器按钮
	private JButton exit = new JButton("关闭服务器");
	
	//保存端口号
	private int post;
	
	//显示端口号
	private JLabel show;
	
	//接收消息线程 
	private ReceiveNews receive;
	
	//服务器Socket
	private ServerSocket server;
	
	public MainFrame(int post) {
		//初始化
		this.post = post;
		
		try {
			server = new ServerSocket(post);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		show = new JLabel("服务器已启动端口号:" + post);
		init();
		
		//接收消息线程启动
		receive = new ReceiveNews(server);
		receive.start();
	}
	
	private void init(){
		
		//设置窗口名称
		this.setTitle("服务器");
		
		//窗口大小固定
		this.setResizable(false);
		
		//设置窗口关闭
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//设置字体
		exit.setFont(new Font("宋体",1,18));
		show.setFont(new Font("宋体",1,18));
		
		//添加监听
		exit.addActionListener(this);
		
		//设置布局
		this.setLayout(null);
		this.setBounds(700,300,300,500);
		exit.setBounds(70,300,150,30);
		show.setBounds(20,150,300,90);
		
		//添加组件
		this.add(exit);
		this.add(show);
		
		//窗体可见
		this.setVisible(true);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		JButton button = (JButton) e.getSource();
		if(button.equals(exit)){//退出
			System.exit(0);
		}
	}
	
}
